package com.bj.dljd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.bj.dljd.convent.ConvertUserConvert;
import com.bj.dljd.domain.UserInfo;
import com.bj.dljd.models.query.UserEditQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.models.vo.UserInfoVo;
import com.bj.dljd.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.bj.dljd.constant.SystemConstant.PAGE_SIZE;

@CrossOrigin
@RestController
public class UserInfoController {
    @Resource
    private UserService userService;

    @Resource
    private ConvertUserConvert convertUserConvert;

    @GetMapping("/user/pageList")
    public R pageList(@RequestParam Integer pageNo){
        pageNo = (pageNo == null || pageNo < 1 ) ? 1 : pageNo;

        Page<UserInfo> page = userService.pageList(pageNo, PAGE_SIZE);
        // 总页数
        long pages = page.getPages() == 0 ? 1 : page.getPages();

        // 查询记录集合
        List<UserInfo> records = page.getRecords();

        // bean 包含 totalPage userInfoList数据
        Map<String , Object> map = new HashMap<>();
        map.put("totalPage",pages);
        map.put("list",convertUserConvert.convertListUserInfoVo(records));

        //休眠
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 返回数据

       return R.Ok(map);
    }

    /*注册后修改商户*/
    @PostMapping("/user/edit")
    public R editUser(@RequestBody @Validated UserEditQuery userEditQuery){

        // 调用service层
        userService.editUser(userEditQuery);
        // 返回R对象
        return R.Ok();
    }
    /*按userNo查询*/
    @GetMapping("/user/queryByUserNo")
    public R queryByUserNo(@RequestParam String userNo){
        if (StrUtil.isNotBlank(userNo)){
            UserInfo userInfo = userService.querByUserNo(userNo);
            UserInfoVo userInfoVo = convertUserConvert.mapUserInfoVo(userInfo);
            return R.Ok(userInfoVo);

        }
        return R.fuil("请求参数异常");
    }

    /**
     *
     * @param userNo
     * @return
     */
    @PostMapping("/user/audit")
    public R audit(@RequestParam String userNo){
        if (StrUtil.isNotBlank(userNo)){
            // 状态修改
            userService.modifyStatusByUserNo(userNo);
            return R.Ok();
        }
        return R.fuil("请求参数异常");

    }

}
